package cn.edu.ujn.multiple.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.ujn.multiple.model.Customer;
import cn.edu.ujn.multiple.model.Orders;
import cn.edu.ujn.multiple.model.Product;
import cn.edu.ujn.multiple.service.ICustomerService;
import cn.edu.ujn.multiple.service.IOrdersService;
import cn.edu.ujn.multiple.service.IProductService;

@Controller
public class OrdersController {
	@Autowired
	private IOrdersService ordersService;
	@Autowired
	private ICustomerService customerService;
	@Autowired
	private IProductService productService;
	
	
	@GetMapping("/orders")
	public String toOrder(Model model) {
		Customer associationCustomer = customerService.findByPrimaryKey(1);
		model.addAttribute("associationCustomer", associationCustomer);
		return "orders";
	}
	
	@RequestMapping("/findAllOrders")
	public String findAll(Model m) {
		List<Orders> list = ordersService.findAll();
		m.addAttribute("ordersList", list);
		return "orders";
	}
	
	@GetMapping("/orders/list.action")
	public String query(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows,
			String custName, String custSource, String custIndustry, String custLevel, Model m) {

		m.addAttribute("custName", custName);
		m.addAttribute("custSource", custSource);
		m.addAttribute("custIndustry", custIndustry);
		m.addAttribute("custLevel", custLevel);

		List<Orders> list = ordersService.selectByCustId(rows);
		
		m.addAttribute("page", list);

		return "orders";
	}
	
	@GetMapping("/addOrder")
	public String addOrder(Model m) {
		List<Product> list = productService.findAll();
		m.addAttribute("products", list);
		return "addOrder";
	}
	
}
